/*
 * Contains Student_Report class
 */
package student_report;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Student report data java representation
 * @author Alexey Akimov <frinatella@gmail.com>
 */
public class Student_Report {

    /**
     * Constructor
     * Initialises STUDENTS, SUBJECTS and MARKS tables java representation sets
     */
    public Student_Report() {
        this.students = new HashMap<Integer, Student>() {};
        this.subjects = new HashMap<Integer, Subject>();
        this.marks = new HashMap<Integer, Mark>();
    }
    
    /**
     * STUDENTS table java representation
     */
    private HashMap<Integer, Student> students;
    
    /**
     * SUBJECTS table java representation
     */
    private HashMap<Integer, Subject> subjects;
    
    /**
     * MARKS table java representation
     */
    private HashMap<Integer, Mark> marks;
    
    /**
     * Saves student report java representation content to datebase
     */
    public void save() {
        try {
            Connection connection = DbConnection.getConnection();
            PreparedStatement statement = connection.prepareStatement("insert into STUDENTS (STUDENT_ID, SURNAME, NAME, GROUP_NUMBER) values(?, ?, ?, ?)");
            Student[] studentsArray = students.values().toArray(new Student[0]);
            for(Student student : studentsArray) {
                statement.setInt(1, student.getStudentId());
                statement.setString(2, student.getSurname());
                statement.setString(3, student.getName());
                statement.setInt(4, student.getGroupNumber());
                statement.executeUpdate();
            }
            statement.close();
            statement = connection.prepareStatement("insert into SUBJECTS (SUBJECT_ID, SUBJECT_NAME) values(?, ?)");
            Subject[] subjectsArray = subjects.values().toArray(new Subject[0]);
            for(Subject subject : subjectsArray) {
                statement.setInt(1, subject.getSubjectId());
                statement.setString(2, subject.getSubjectName());
                statement.executeUpdate();
            }
            statement.close();
            statement = connection.prepareStatement("insert into MARKS (MARK_ID, STUDENT_ID, SUBJECT_ID, MARK_VALUE) values(?, ?, ?, ?)");
            Mark[] marksArray = marks.values().toArray(new Mark[0]);
            for(Mark mark : marksArray) {
                statement.setInt(1, mark.getMarkId());
                statement.setInt(2, mark.getStudentId());
                statement.setInt(3, mark.getSubjectId());
                statement.setInt(4, mark.getMarkValue());
                statement.executeUpdate();
            }
            statement.close();
        } catch (SQLException ex) {
            Logger.getLogger(Student_Report.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
